package com.huluobo.kotlinim2006a.ui.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.kotlinim2006a.R
import com.huluobo.kotlinim2006a.adapter.ContactListAdapter
import com.huluobo.kotlinim2006a.adapter.EMContactListenerAdapter
import com.huluobo.kotlinim2006a.base.BaseFragment
import com.huluobo.kotlinim2006a.contract.contact.ContactContract
import com.huluobo.kotlinim2006a.contract.contact.ContactPresenter
import com.huluobo.kotlinim2006a.ui.activity.AddFriendActivity
import com.huluobo.kotlinim2006a.widget.SlideBar
import com.hyphenate.chat.EMClient
import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * Created by lc on 2023/2/3.
 */
class ContactFragment : BaseFragment(), ContactContract.View {
    override fun getLayoutResId(): Int = R.layout.fragment_contacts

    val presenter = ContactPresenter(this)

    override fun init() {
        super.init()
        headerTitle.text = getString(R.string.contact)
        add.visibility = View.VISIBLE

        add.setOnClickListener {
            context?.startActivity<AddFriendActivity>()
        }

//        swipeRefreshLayout.setColorSchemeColors(resources.getColor(R.color.qq_blue))
//        swipeRefreshLayout.isRefreshing = true

        swipeRefreshLayout.apply {
            setColorSchemeColors(resources.getColor(R.color.qq_blue))
            isRefreshing = true
            setOnRefreshListener {
                presenter.loadContacts()
            }
        }

//        recyclerView.layoutManager = LinearLayoutManager(context)
//        recyclerView.adapter = ContactListAdapter(context!!)

        recyclerView.apply {
            setHasFixedSize(true)//当Recyclerview的长度发生变化是,内部会做优化处理
            layoutManager = LinearLayoutManager(context)
            adapter = ContactListAdapter(context, presenter.contactListItems)
        }

        presenter.loadContacts()

        EMClient.getInstance().contactManager()
            .setContactListener(object : EMContactListenerAdapter() {
                override fun onContactDeleted(username: String?) {
                    //感知到好友被删除
                    presenter.loadContacts()
                }
            })

        slideBar.onSectionChangeListener = object : SlideBar.OnSectionChangeListener {
            override fun onSectionChange(firstLetter: String) {
                section.visibility = View.VISIBLE
                section.text = firstLetter
                moveToLetterPosition(firstLetter)
            }

            override fun onSlideFinish() {
                section.visibility = View.GONE
            }

        }
    }

    private fun moveToLetterPosition(firstLetter: String) {
        presenter.contactListItems.forEachIndexed { index, it ->
            if (it.firstLetter.toString() == firstLetter) {
                recyclerView.smoothScrollToPosition(index) //smoothScrollToPosition平滑移动到Position的位置
            }
        }
    }

    override fun onLoadContactsSuccess() {
        //关闭下拉刷新
        swipeRefreshLayout.isRefreshing = false
        //刷新适配器
        recyclerView.adapter?.notifyDataSetChanged()
    }

    override fun onLoadContactsFailed() {
        //关闭下拉刷新
        swipeRefreshLayout.isRefreshing = false
        //toast提示
        context?.toast(R.string.load_contacts_failed)
    }
}